import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../network/http_request.dart';
import '../../../utils/common_widget.dart';
import '../../../utils/loading.dart';
import '../../../utils/provider.dart';
import 'address_chose_page.dart';
import 'package:get/get.dart';
import 'address_manage_controller.dart';
import 'adress_chose_poi_model.dart';

class AddressManagePage extends StatefulWidget {
  Map<String, dynamic>? resMap;
  Function? refreshBlock;
  bool isFromUpload = false;
  Function? callBackAddressBlock;

  AddressManagePage(
      {Key? key,
      this.resMap,
      this.refreshBlock,
      this.callBackAddressBlock,
      this.isFromUpload = false})
      : super(key: key);

  @override
  State<AddressManagePage> createState() => _AddressManagePageState();
}

class _AddressManagePageState extends State<AddressManagePage> {
  double cellHeight = 50;
  double lineHeight = 0.5;
  TextStyle textStyle = TextStyle(
    fontSize: 16,
    color: ThemeColor.black1414Color,
  );
  AddressManageController addressManageController =
      Get.put(AddressManageController());
  AddressChosePoiDataModel saveModel = AddressChosePoiDataModel();

  @override
  Widget build(BuildContext context) {
    return AppWidget.navTitleBar(
        title: '地址',
        childWidget: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 14),
          child: Column(
            children: [
              CupertinoButton(
                padding: EdgeInsets.zero,
                onPressed: () {
                  Get.to(AddressChosePage(
                    finishBlock: (AddressChosePoiDataModel choseModel) {
                      String proStr =
                          "${choseModel.pname ?? ''} ${choseModel.cityname ?? ''} ${choseModel.adname ?? ''}";
                      if (choseModel.pname == choseModel.cityname) {
                        proStr =
                            "${choseModel.cityname ?? ''} ${choseModel.adname ?? ''}";
                      }
                      String addressStr =
                          "${choseModel.address ?? ''}${choseModel.name ?? ''}";
                      addressManageController.provinceVC.text = proStr;
                      addressManageController.addressVC.text = addressStr;
                      addressManageController.update([
                        addressManageController.provinceViewId,
                        addressManageController.detailsViewId,
                      ]);
                      saveModel = choseModel;
                    },
                  ));
                },
                child: Container(
                  height: 45,
                  decoration: BoxDecoration(
                    border: Border.all(color: ThemeColor.appColor, width: 0.8),
                    borderRadius: BorderRadius.circular(5),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        '定位机构地址',
                        style:
                            TextStyle(color: ThemeColor.appColor, fontSize: 16),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(top: 3),
                        child: Icon(
                          Icons.arrow_forward_ios_outlined,
                          color: ThemeColor.appColor,
                          size: 19,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              GetBuilder(
                  id: addressManageController.provinceViewId,
                  builder: (AddressManageController controller) {
                    return getLeftNameAndRightArrow(
                        leftName: '所在地址',
                        hintName: '河北省 承德市 双桥区',
                        isEnabled: false,
                        isHasArrow: false,
                        bindController: controller.provinceVC);
                  }),
              GetBuilder(
                  id: addressManageController.detailsViewId,
                  builder: (AddressManageController controller) {
                    return getLeftNameAndRightArrow(
                        leftName: '详细地址',
                        hintName: '街道、楼牌号',
                        bindController: controller.addressVC);
                  }),
              const SizedBox(
                height: 20,
              ),
              CupertinoButton(
                onPressed: () {
                  subMitAddressInfo();
                },
                child: Container(
                  height: 45,
                  decoration: BoxDecoration(
                    color: ThemeColor.appColor,
                    borderRadius: BorderRadius.circular(25),
                  ),
                  alignment: Alignment.center,
                  child: const Text(
                    '保存地址',
                    style: TextStyle(fontSize: 16, color: Colors.white),
                  ),
                ),
              ),
            ],
          ),
        ));
  }

  Widget getLeftNameAndRightArrow(
      {String leftName = '',
      String hintName = '',
      bool isEnabled = true,
      bool isHasArrow = true,
      required TextEditingController bindController}) {
    return SizedBox(
      height: cellHeight,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          SizedBox(
            height: cellHeight - lineHeight,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  leftName,
                  style: textStyle,
                ),
                const SizedBox(
                  width: 10,
                ),
                Expanded(
                  child: TextField(
                    controller: bindController,
                    enabled: isEnabled,
                    style: textStyle,
                    cursorColor: ThemeColor.appColor,
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      isCollapsed: true,
                      hintStyle: TextStyle(
                          color: ThemeColor.hint777777Color, fontSize: 14),
                      hintText: hintName,
                    ),
                  ),
                ),
                Visibility(
                  visible: isHasArrow,
                  child: Icon(
                    Icons.arrow_forward_ios,
                    color: ThemeColor.textColorSecond,
                    size: 15,
                  ),
                ),
              ],
            ),
          ),
          Container(
            height: lineHeight,
            color: ThemeColor.lineColor,
          ),
        ],
      ),
    );
  }

  subMitAddressInfo() async {
    if (widget.isFromUpload) {
      if (widget.callBackAddressBlock != null) {
        widget.callBackAddressBlock!(
            saveModel.pname,
            saveModel.cityname,
            saveModel.adname,
            addressManageController.addressVC.text,
            saveModel.longitude,
            saveModel.latitude);
      }
    } else {
      Map<String, dynamic> datMap = {
        "memberId": Provider.memberId,
        'strLon': saveModel.longitude,
        'strLat': saveModel.latitude,
        'province': saveModel.pname,
        'city': saveModel.cityname,
        'area': saveModel.adname,
        'address': addressManageController.addressVC.text
      };
      var res = await HttpRequest.request('match-maker/business/uploadLonLat',
          params: datMap);
      if (res['code'] == 0) {
        final data = res['data'];
        if (data != null) {
          toast('保存成功');
          if (widget.refreshBlock != null) {
            widget.refreshBlock!();
          }
          Get.back();
        } else {
          toast(res['msg']);
        }
      }
    }
    Get.back();
  }

  @override
  void initState() {
    super.initState();
    if (widget.resMap != null) {
      if (widget.resMap!['address'] != null) {
        addressManageController.addressVC.text = widget.resMap!['address'];
      }

      if (widget.resMap!['area'] != null) {
        saveModel.pname = widget.resMap!['province'];
        saveModel.cityname = widget.resMap!['city'];
        saveModel.adname = widget.resMap!['area'];
        if (saveModel.pname == saveModel.cityname) {
          addressManageController.provinceVC.text =
              '${saveModel.cityname} ${saveModel.adname}';
        } else {
          addressManageController.provinceVC.text =
              '${saveModel.pname} ${saveModel.cityname} ${saveModel.adname}';
        }
      }

      if (widget.resMap!['longitude'] != null) {
        saveModel.longitude = widget.resMap!['longitude'];
      }

      if (widget.resMap!['latitude'] != null) {
        saveModel.latitude = widget.resMap!['latitude'];
      }
    }
  }
}
